package xin.ssm.jxc.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import xin.ssm.jxc.model.Companys;
import xin.ssm.jxc.services.ICompanyService;
import xin.ssm.jxc.util.Result;

@Controller
@RequestMapping("/company")
public class CompanyController {
	@Resource
	private ICompanyService companyService;
	// 动态查询所有客户
	@RequestMapping("/findcompanydynamic")
	@ResponseBody
	public Result findCompanyDynamic(
			@RequestParam(value = "companyname") String companyname,
			@RequestParam(value = "companyaddress") String companyaddress,
			@RequestParam(value = "companylinkman") String companylinkman) {
		if(companyname.equals("") || companyname.isEmpty()) {
			companyname=null;
		}
		if(companyaddress.equals("") || companyaddress.isEmpty()) {
			companyaddress=null;
		}
		if(companylinkman.equals("") || companylinkman.isEmpty()) {
			companylinkman=null;
		}
		Companys c = new Companys();
//		c.setCompanyid(Integer.parseInt(companyid));
		c.setCompanyname(companyname);
		c.setCompanyaddress(companyaddress);
		c.setCompanylinkman(companylinkman);
		List<Companys> userList = companyService.findCompanyDynamic(c);
		// 时间转换
		String jsonarry = JSONArray.toJSONStringWithDateFormat(userList, "yyyy-MM-dd");
		Result result = new Result();
		result.setCode(0);
		result.setMsg("成功");
		result.setCount(userList.size());
		result.setData(userList);
		return result;
	}
	//页面查询准备数据
			@RequestMapping("/findallcompany")
			@ResponseBody
			public List<Companys> findEmployee() {
				List<Companys> list = companyService.findAllCompany();
				return list;
			}
	
	// 删除员工
			@RequestMapping("/deletecompany")
			@ResponseBody
			public String deleteUsers(@RequestParam(value = "companyid") String companyid) {
				int ret = companyService.deleteCompany(Integer.parseInt(companyid));
				if (ret > 0) {
					return "1";
				} else {
					return "0";
				}
			}
			
			// 修改客户
			@RequestMapping("/updatecompanydynamic")
			@ResponseBody
			public String updateUsers(
					@RequestParam(value = "companyid") String companyid,
					@RequestParam(value = "companyname") String companyname,
					@RequestParam(value = "companyaddress") String companyaddress,
					@RequestParam(value = "companylinkman") String companylinkman,
					@RequestParam(value = "companytel") String companytel) {
				Companys c = new Companys();
				c.setCompanyid(Integer.parseInt(companyid));
				c.setCompanyname(companyname);
				c.setCompanyaddress(companyaddress);
				c.setCompanylinkman(companylinkman);
				c.setCompanytel(companytel);
				int ret = companyService.updateCompanyDynamic(c);
				if (ret > 0) {
					return "1";
				} else {
					return "0";
				}
			}
			// 添加客户
			@RequestMapping("/addcompany")
			@ResponseBody
			public String addUsers(
					@RequestParam(value = "companyname") String companyname,
					@RequestParam(value = "companyaddress") String companyaddress,
					@RequestParam(value = "companylinkman") String companylinkman,
					@RequestParam(value = "companytel") String companytel) {
				Companys c = new Companys();
				c.setCompanyname(companyname);
				c.setCompanyaddress(companyaddress);
				c.setCompanylinkman(companylinkman);
				c.setCompanytel(companytel);
				int ret = companyService.addCompany(c);
				if (ret > 0) {
					return "1";
				} else {
					return "0";
				}
			}			
}
